package com.congwulin.wechat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.congwulin.wechat.service.WeChatWeatherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "weather")
public class WeatherController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WeChatWeatherService service;

    @RequestMapping(value = "/getNow",method = RequestMethod.GET)
    public@ResponseBody
    String getNow(String city_name){
//        System.out.println(city_name);
        JSONObject json = service.getCityId("sbbphacqgmqhxqoh",city_name);
        System.out.println(json.toJSONString());
        JSONArray results = json.getJSONArray("results");
        String id = results.getJSONObject(0).getString("id");
//        if(results.size()>1){
//            String cityAndregion = region_name+","+city_name;
//            for(int i=0;i<results.size();i++){
//                if(cityAndregion.equals(results.getJSONObject(i).getString("path").substring(0,cityAndregion.length()))){
//                    id = results.getJSONObject(i).getString("id");
//                    break;
//                }
//            }
//        }else{
//
//        }
        json = service.getNowWeather("sbbphacqgmqhxqoh",id,"zh-Hans","c");
        return json.toJSONString();
//        return "success11111";
    }

    @RequestMapping(value = "/getTest",method = RequestMethod.GET)
    public@ResponseBody
    String getTest(){
        return "success";
    }
}

